/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.infra.run;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import edu.cmu.lti.sepia.data.xml.IXmlHandler;
import edu.cmu.lti.sepia.infra.run.model.AnswerType;
import edu.cmu.lti.sepia.infra.run.model.QuestionAnalysisResult;
import edu.cmu.lti.sepia.infra.run.model.QuestionAnalysisResultSet;
import edu.cmu.lti.sepia.infra.run.model.Term;

public class QuestionAnalysisXmlToObj extends DefaultHandler implements IXmlHandler {
	
	enum XML {
		TOPIC, ANSWERTYPE, KEYTERM, ID, SCORE, METADATA, RUNID, DESCRIPTION
	}
	
	private QuestionAnalysisResultSet result = new QuestionAnalysisResultSet();
	
	private boolean inRunid = false;
	private boolean inDescription = false;
	
	private boolean inAnswerType = false;
	private boolean inKeyterm = false;
	
	private String currentId;
	private QuestionAnalysisResult currentResult;
	private double currentTypeScore;
	private double currentTermScore;
	
	private StringBuilder buffer = new StringBuilder();
	
	@Override
	public void startElement( String uri, String localName, 
			String qName, Attributes attributes ) {
	  if ( XML.RUNID.toString().equals(qName) ) {
	    inRunid = true;
	    buffer = new StringBuilder();
	  } else if ( XML.DESCRIPTION.toString().equals(qName) ) {
	    inDescription = true;
	    buffer = new StringBuilder();
	  } else if ( XML.TOPIC.toString().equals(qName) ) {
			currentResult = new QuestionAnalysisResult();
			currentId = attributes.getValue(XML.ID.toString());
		} else if ( XML.ANSWERTYPE.toString().equals(qName) ) {
			inAnswerType = true;
			buffer = new StringBuilder();
			String scoreText = attributes.getValue(XML.SCORE.toString());
			if (scoreText!=null) {
			  currentTypeScore = Double.parseDouble( scoreText );
			} else {
			  currentTypeScore = 1.0D;
			}
		} else if ( XML.KEYTERM.toString().equals(qName) ) {
			inKeyterm = true;
			buffer = new StringBuilder();
			String scoreText = attributes.getValue(XML.SCORE.toString());
			if (scoreText!=null) {
			  currentTermScore = Double.parseDouble( scoreText );
      } else {
        currentTermScore = 1.0D;
      }
		}
	}
	
	@Override
	public void characters(char[] ch, int offset, int length) {
	  if (inRunid||inDescription||inAnswerType||inKeyterm) {
	    String value = new String(ch, offset, length);
	    buffer.append(value);
	  }
	}
	
	@Override
	public void endElement( String uri, String localName, String qName ) {
   if (inRunid) {
      result.getMetadata().put("RUNID", buffer.toString());
      inRunid = false;
    } else if (inDescription) {
      result.getMetadata().put("DESCRIPTION", buffer.toString());
      inDescription = false;
    } if ( inAnswerType ) {
      AnswerType type = new AnswerType(buffer.toString(), currentTypeScore);
      currentResult.addAnswerType( type );
      inAnswerType = false;
    } else if ( inKeyterm ) {
      Term t = new Term(buffer.toString(), currentTermScore);
      currentResult.addTerm( t );
      inKeyterm = false;
    }
    
    // end of file
	  if ( XML.TOPIC.toString().equals(qName) ) {
			result.put(currentId, currentResult);
		}
	}
	
	public QuestionAnalysisResultSet getParsed() {
	  return result;
	}
	
}
